package service;

import android.content.Context;
import android.util.Log;

import com.sxpjyy.monkey.mobilenurse.R;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class WebService {
    private String METHOD_NAME = null;

    private String[] PARMS = null;

    private Context mContext;

    public WebService(Context context) {
        if (context != null) {
            mContext = context;
            Log.d("tag", "webservice 初始化成功！");
        }
    }

    public void setPARMS(String[] PARMS) {
        this.PARMS = PARMS;
    }

    public void setMETHOD_NAME(String METHOD_NAME) {
        this.METHOD_NAME = METHOD_NAME;
    }

    /**
     * 调用java写的WebService
     */
    public String getServerData() {
        String result = null;
        String NAME_SPACE = mContext.getString(R.string.name_space);
        String soapAction = NAME_SPACE + "/" + METHOD_NAME;
        SoapObject soapObject = new SoapObject(NAME_SPACE, METHOD_NAME);
        if (PARMS != null && PARMS.length > 0) {
            for (int i = 0; i < PARMS.length; i++) {
                soapObject.addProperty("arg" + i, PARMS[i]);
            }
        }
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = soapObject;
//        String END_POINT = mContext.getString(R.string.end_point);
        String END_POINT = mContext.getString(R.string.test_end_point);
        HttpTransportSE transportSE = new HttpTransportSE(END_POINT);
        try {
            transportSE.call(soapAction, envelope);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SoapObject resultObject = (SoapObject) envelope.bodyIn;
        if (resultObject != null && resultObject.getProperty(0) != null) {
            result = resultObject.getProperty(0).toString();
        }
        return result;
    }
}